﻿using System;

namespace Property.Reloaders
{
    public partial class StateConditionReloaderDecoratorPrototype<T, TState>
    {
        private class DecoratorInstance : ReloaderDecorator<T>, IReloader
        {
            public DecoratorInstance(IReloaderPrototype<T> compositionBasePrototype,
                Func<T> getValue, Action<T> applyValue, Action<bool> applyStatus,
                Func<TState, TState> getConditionState, Func<TState, bool> condition)
                : base(compositionBasePrototype, getValue, applyValue, applyStatus)
            {
                if (getConditionState == null) throw new ArgumentNullException("getConditionState");
                if (condition == null) throw new ArgumentNullException("condition");

                _GetConditionState = getConditionState;
                _Condition = condition;
            }

            //=//

            private readonly Func<TState, TState> _GetConditionState;
            private readonly Func<TState, bool> _Condition;

            private TState ConditionState;

            public override void Go()
            {
                var condition = _Condition(ConditionState);
                ConditionState = _GetConditionState(ConditionState);
                if (condition) _CompositionBase.Go();
            }
        }
    }
}
